package com.zzy.database.service.Impl;

import com.zzy.database.mapper.LoanMapper;
import com.zzy.database.pojo.Loan;
import com.zzy.database.pojo.LoanPayment;
import com.zzy.database.service.LoanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class LoanServiceImpl implements LoanService {

    @Autowired
    private LoanMapper loanMapper;

    @Override
    @Transactional
    public boolean applyLoan(Loan loan) {
        System.out.println("按理说可以了啊");
        loan.setLoanId(UUID.randomUUID().toString());
        loan.setIssueDate(new Date());
        loan.setLoanStatus("未还");

        return loanMapper.createLoan(loan) > 0;
    }

    @Override
    @Transactional
    public boolean makePayment(LoanPayment loanPayment) {
        Loan loan = loanMapper.getLoanById(loanPayment.getLoanId());
        if (loan == null || "已还".equals(loan.getLoanStatus())) {
            return false;
        }
        return loanMapper.updateLoanStatus(loan.getLoanId(),loan.getAmount())>0;



    }

    @Override
    public List<Loan> getLoanStatus(String loanId) {
        System.out.println("???");
        List<Loan> didigugu=loanMapper.getLoansByCustomerId(loanId);
        System.out.println(didigugu.size());
        return didigugu;
    }

    @Override
    public List<LoanPayment> getLoanPayStatus(String loanpayid) {
        return loanMapper.getLoanPayById(loanpayid);
    }

}
